package com.xing.reggie.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xing.reggie.common.CustomException;
import com.xing.reggie.dto.SetmealDto;
import com.xing.reggie.entity.Setmeal;
import com.xing.reggie.entity.SetmealDish;
import com.xing.reggie.mapper.SetmealMapper;
import com.xing.reggie.service.SetmealDishService;
import com.xing.reggie.service.SetmealService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;

@Service
public class SetmealServiceImpl extends ServiceImpl<SetmealMapper, Setmeal> implements SetmealService {

    @Autowired
    private SetmealDishService setmealDishService;


    /**
     * 新增套餐同时新增对应套餐中的菜品关系
     * @param setmealDto
     */
    @Override
    @Transactional
    public void saveWithDish(SetmealDto setmealDto) {
        //新增套餐
        this.save(setmealDto);
        //新增对应关系
        List<SetmealDish> setmealDishes = setmealDto.getSetmealDishes();
        setmealDishes.stream().map((item)->{
            item.setSetmealId(setmealDto.getId());
            return item;

        }).collect(Collectors.toList());

        setmealDishService.saveBatch(setmealDishes);

    }

    /**
     * 删除套餐同时删除对应套餐中的菜品关系
     * @param ids
     */
    @Override
    @Transactional
    public void deleteWithDish(List<Long> ids) {
        //1.删除套餐信息
        //查看状态，判断是否可以删除
        LambdaQueryWrapper<Setmeal> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        //添加条件
        lambdaQueryWrapper.eq(Setmeal::getStatus,1);
        lambdaQueryWrapper.in(Setmeal::getId,ids);
        //查看满足条件的数量
        int count = this.count(lambdaQueryWrapper);
        if (count >0){
            throw new CustomException("套餐正在售卖中，不能删除！");
        }
        this.removeByIds(ids);
        //2.删除套餐对应菜品关系
        //条件构造器
        LambdaQueryWrapper<SetmealDish> queryWrapper = new LambdaQueryWrapper<>();
        //添加条件
        queryWrapper.in(SetmealDish::getDishId,ids);
        //删除
        setmealDishService.remove(queryWrapper);

    }

    /**
     * 修改套餐同时修改对应套餐中的菜品关系
     * @param setmealDto
     */
    @Override
    @Transactional
    public void updateWithDish(SetmealDto setmealDto) {
        //修改套餐信息
        this.updateById(setmealDto);
        //清楚套餐对应菜品关系并新增关系
        LambdaQueryWrapper<SetmealDish> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SetmealDish::getDishId,setmealDto.getId());
        setmealDishService.remove(queryWrapper);
        //新增
        List<SetmealDish> setmealDishes = setmealDto.getSetmealDishes();
        //处理集合
        Long id = setmealDto.getId();
        setmealDishes = setmealDishes.stream().map((item)->{
            item.setSetmealId(id);
            return item;
        }).collect(Collectors.toList());
        setmealDishService.saveBatch(setmealDishes);

    }


    /**
     * 通过id查看套餐及对应菜品信息
     * @param id
     * @return
     */
    @Override
    public SetmealDto getByIdWithDish(Long id) {
        //通过id查看套餐信息
        Setmeal setmeal = this.getById(id);
        //得到套餐对应的菜品信息
        SetmealDto setmealDto = new SetmealDto();
        //拷贝
        BeanUtils.copyProperties(setmeal,setmealDto);

        LambdaQueryWrapper<SetmealDish> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SetmealDish::getSetmealId,id);
        List<SetmealDish> list = setmealDishService.list(queryWrapper);


        setmealDto.setSetmealDishes(list);

        return setmealDto;
    }
}
